package demo16;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电话号码的字母组合
 * https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/
 */

class Solution1 {
    Map<Integer, char[]> map = new HashMap<>();
    List<String> ret = new ArrayList<>();
    int len;
    StringBuffer path = new StringBuffer();

    public List<String> letterCombinations(String digits) {
        map.put(2, new char[]{'a', 'b', 'c'});
        map.put(3, new char[]{'d', 'e', 'f'});
        map.put(4, new char[]{'g', 'h', 'i'});
        map.put(5, new char[]{'j', 'k', 'l'});
        map.put(6, new char[]{'m', 'n', 'o'});
        map.put(7, new char[]{'p', 'q', 'r', 's'});
        map.put(8, new char[]{'t', 'u', 'v'});
        map.put(9, new char[]{'w', 'x', 'y', 'z'});
        char[] chars = digits.toCharArray();
        len = chars.length;
        if (len == 0) return ret;
        dfs(chars, 0);
        return ret;
    }

    private void dfs(char[] chars, int pow) {
        if (path.length() == len) {
            ret.add(path.toString());
            return;
        }
        for (int i = pow; i < len; i++) {
            char[] chs1 = map.get(chars[i] - '0');
            for (char ch1 : chs1) {
                path.append(ch1);
                dfs(chars,i+1);
                path.deleteCharAt(path.length() - 1);
            }
        }
    }
}

class Demo1 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        List<String> list = solution.letterCombinations("23");
        for (String str : list) {
            System.out.println(str);
        }
    }
}